package cn.itcast.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Xiao
 * @version 1.0
 * @description 配置普通消息交换机队列
 * @date 2021/11/19
 */
@Configuration
public class CommonConfig {
    private static final String SIMPLE_EXCHANGE = "simple.direct";
    private static final String SIMPLE_QUEUE = "simple.queue";
    private static final String SIMPLE_TEST_KEY = "simple.test";
    @Bean
    public DirectExchange simpExchange() {
        //三个参数：交换机名称、是否持久化、当没有queue与其绑定时是否自动删除
        //默认情况下，由SpringAMQP声明的交换机都是持久化的。
        return new DirectExchange(SIMPLE_EXCHANGE, true, false);
    }
    @Bean
    public Queue simpleQueue() {
        //使用QueueBuilder构建队列，durable就是持久化的
        //默认情况下，由SpringAMQP声明的队列都是持久化的。
        return QueueBuilder.durable(SIMPLE_QUEUE).build();
    }
    @Bean
    public Binding simpleQueueBinding() {
        return BindingBuilder.bind(simpleQueue()).to(simpExchange()).with(SIMPLE_TEST_KEY);
    }
}
